// kuaidi/pages/rider/user/index.js
import Dialog from '@vant/weapp/dialog/dialog';
const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    userInfo: [],
    is_cash: false,
    cash_value: 0,
    to_value: 0
  },
  onChangeTabs(e) {
    let _this = this
    _this.setData({
      active: e.detail.index
    })
    _this.getOrder()
  },
  getOrder() {
    let _this = this;
    App._get('kuaidi.rider.order/all', {
      active: _this.data.active
    }, function (result) {
      _this.setData(result.data)
    });
  },
  /**
   * 获取当前用户信息
   */
  getUserDetail() {
    let _this = this;
    App._get('user.index/detail', {}, function (result) {
      _this.setData({
        userInfo: result.data.userInfo
      });
    });
  },
  onChangeTabbar(e) {
    App.riderTabbar(e.detail)
  },
  onChangeCash(e) {
    let _this = this
    _this.setData({
      cash_value: e.detail
    })
    setTimeout(() => {
      _this.calcCashValue()
    }, 500);
  },
  onCash() {
    let _this = this
    _this.setData({
      cash_value: _this.data.userInfo.rider.balance
    })
    _this.isCash()
    _this.calcCashValue()
  },
  isCash() {
    let _this = this
    _this.setData({
      is_cash: !_this.data.is_cash
    })
  },
  calcCashValue() {
    let _this = this
    let balance = _this.data.userInfo.rider.balance
    let percentage = _this.data.userInfo.rider.percentage
    let cash_value = _this.data.cash_value
    if (parseFloat(cash_value) > parseFloat(balance)) {
      _this.setData({
        cash_value: _this.data.userInfo.rider.balance
      })
    }
    let to_value = _this.data.cash_value * (percentage / 100)
    _this.setData({
      to_value: to_value
    })
  },
  onConfirmCash(e) {
    let _this = this
    App._post_form('kuaidi.rider.User/withdraw', {
      money: _this.data.cash_value
    }, result => {
      // 获取用户详情
      _this.getUserDetail()
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this
    // 获取用户详情
    _this.getUserDetail()
    // 获取订单信息
    _this.getOrder()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})